<?php namespace dmq;

abstract class View
{
	private $markup;		// (string), Markup buffer.
	
	protected $messages;	// (array), "Success" messages.
	protected $errors;		// (array), Error messages.
	protected $auth;		// (Auth), User authentication.
	
	final public function __construct()
	{
		$this->markup = '';
		$this->messages = array();
		$this->errors = array();

		require_once Settings::DMQ_PATH . 'Auth.php';
		$this->auth = new Auth();
	}
	
	final public function markup( $str = false )
	{
		if ( $str === false )
		{
			return $this->markup;
		}
		else
		{
			$this->markup .= $str;
		}
	}
	
	final public function controller()
	{
		return isset( $_GET['c'] ) && preg_match( '/^[a-z0-9_\-]+$/i', $_GET['c'] ) ? ucfirst( strtolower( $_GET['c'] ) ) : null;
	}

	final public function action()
	{
		return isset( $_GET['a'] ) && preg_match( '/^[a-z0-9_\-]+$/i', $_GET['a'] ) ? ucfirst( strtolower( $_GET['a'] ) ) : null;
	}

	final public function auth()
	{
		return $this->auth;
	}

	final public function message( $msg ) {
		$this->messages[] = $msg;
	}
	
	final public function error( $msg )
	{
		$this->errors[] = $msg;
	}

	final public function printErrors( $format = '<p class="error">_ERROR_</p>' )
	{
		foreach ( $this->errors as $error )
		{
			$this->Markup( str_replace( '_ERROR_', $error, $format ) );
		}
	}

	final public function printMessages( $format = '<p class="message">_MESSAGE_</p>' )
	{
		foreach ( $this->messages as $message )
		{
			$this->Markup( str_replace( '_MESSAGE_', $message, $format ) );
		}
	}
}
